<?php
namespace app\admin\controller;

use think\Controller;
//权限管理
class Place extends Common
{
	//权限列表
    public function place_list()
    {
		$where=array('a.id'=>array('gt',0));
		$name=Request()->param('name');
		//权限名称
		$status=Request()->param('status');
		//权限状态太
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
		if($name!=''){
			$where['a.p_name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.p_status']=1;
			}else{
				$where['a.p_status']=array('neq',1);
			}
			
		}
		$place=db('place a')
			->field("a.*,b.p_name as p_name1")
			->join('fr_place b','a.p_id = b.id','left')
			->order('a.p_sort desc,a.p_status,a.id')
			->where($where)
            ->limit("$now,$size")
			->select();
        $count=db('place a')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($place));//分页
		$this->view->assign('page_str',$page_str);
		$this->assign('status',$status);
		$this->assign('name',$name);
        $this->assign('place',$place);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();
		//加载index页面
    }
    //禁用地区
    function place_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('place')
                ->where(array('id'=>$id))
                ->update(array('p_status'=>2));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用地区');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用地区');
            }
        }
    }
    //启用地区
    function place_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('place')
                ->where(array('id'=>$id))
                ->update(array('p_status'=>1));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用地区');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用地区');
            }
        }
    }
    //添加服务地区
    function add_place(){
        if(request()->isGet()){
            $place=db('place')
                ->where(array('p_type'=>array('in','1,2,3'),'p_status'=>1))
                ->order('p_sort desc,id')
                ->select();
            $this->assign('place',$place);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $data['p_name']=trim($data['p_name']);
            if($data['p_name']==''){
                return return_json(array('code'=>0,'msg'=>'请输入地区名称'),'添加服务地区');
            }
            if($data['p_id']>0){
                $p_place=db('place')
                    ->where(array('p_type'=>array('in','1,2,3'),'p_status'=>1,'id'=>$data['p_id']))
                    ->find();
                if(empty($p_place)){
                    return return_json(array('code'=>0,'msg'=>'请选择正确的上级地区'),'添加服务地区');
                };
                $data['p_type']=$p_place['p_type']+1;
            }else{
                $data['p_id']=0;
                $data['p_type']=1;
            }
            if($data['p_sort']<=0){
                $data['p_sort']=0;
            }
            $res=db('place')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'服务地区添加成功'),'添加服务地区');
            }else{
                return return_json(array('code'=>0,'msg'=>'服务地区添加失败'),'添加服务地区');
            }
        }
    }
    //修改服务地区
    function replace(){
        if(request()->isGet()){
            $id=request()->get('id');
            $place=db('place')
                ->where(array('p_type'=>array('in','1,2,3'),'p_status'=>1))
                ->order('p_sort desc,id')
                ->select();
            $t_place=db('place')
                ->where(array('id'=>$id))
                ->find();
            $this->assign('id',$id);
            $this->assign('place',$place);
            $this->assign('t_place',$t_place);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $id=$data['id'];
            unset($data['id']);
            $data['p_name']=trim($data['p_name']);
            if($data['p_name']==''){
                return return_json(array('code'=>0,'msg'=>'请输入地区名称'),'修改服务地区');
            }
            if($data['p_id']>0){
                $p_place=db('place')
                    ->where(array('p_type'=>array('in','1,2,3'),'p_status'=>1,'id'=>$data['p_id']))
                    ->find();
                if(empty($p_place)){
                    return return_json(array('code'=>0,'msg'=>'请选择正确的上级地区'),'修改服务地区');
                };
                $data['p_type']=$p_place['p_type']+1;
            }else{
                $data['p_id']=0;
                $data['p_type']=1;
            }
            if($data['p_sort']<=0){
                $data['p_sort']=0;
            }
            $res=db('place')->where(array('id'=>$id))->update($data);
            //添加权限
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'服务地区修改成功'),'修改服务地区');
            }else{
                return return_json(array('code'=>0,'msg'=>'服务地区修改失败'),'修改服务地区');
            }
        }
    }
    //添加分类
    public function add_place_csv(){
        if(request()->isGet()){
            return $this->view->fetch();
        }else if(request()->isAjax()){
            set_time_limit(0);
            $file=request()->file('csv_file');
            if(!$file){
                return return_json(array('code'=>0,'msg'=>'请选择文件'),'导入服务地区');
            }
            //以下将模板传入第三方
            $upload = new Upload();
            $return=$upload->upload($file);
            if($return['code']!=1){
                return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传模板');
            }else{
                $data['g_img']=$return['url'];
            }
            echo $data['g_img'];die;
            //http://sczxyx-mall.oss-cn-chengdu.aliyuncs.com/a5e6c20190821113542811737.csv
            //模板地址
            //以上将模板传入第三方
            $ext   = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            if($ext!='csv' && $ext!='CSV'){
                return return_json(array('code'=>0,'msg'=>'只支持csv文件'),'导入服务地区');
            }
            $csv_file = fopen($file->getInfo('tmp_name'), 'r');
            //打开文件
            while($data=fgetcsv($csv_file,1000,",")){
                $data = eval('return '.iconv('gbk','utf-8',var_export($data,true)).';');//防止乱码
                //读取有保单号的数据
                $list[] = $data;

            }
            //将文件内容读取未数组
            unset($list[0]);//去除第一行
//            $system=db('system')->find();
            //系统设置
            $all_place=array();
            $place4=array();//第三级分类数组
            foreach($list as $v){
                if($v[1]!='') {
                    if ($v[0] == 1) {
                        //省级服务地区
                        foreach ($all_place as $vv) {
                            //在已有数据中查询，减少数据库查询
                            if ($vv['p_type'] == 1 && $v[1] == $vv['p_name']) {
                                $place = $vv;
                            }
                        }
                        if (empty($place)) {
                            $place = db('place')
                                ->where(array('p_name' => $v[1], 'p_type' => 1))
                                ->find();
                            if (empty($place)) {
                                $place = array('p_name' => $v[1], 'p_type' => 1, 'p_id' => 0, 'p_sort' => $v[4] > 0 ? $v[4] : 0, 'p_status' => $v[3] == '启用' ? 1 : 2);
                                $res = db('place')->insertGetId($place);
                                $place['id'] = $res;
                                $all_place[] = $place;
                            } else {
                                $all_place[] = $place;
                            }
                        }
                        unset($place);
                    } else if ($v[0] == 2) {
                        //市级服务地区
                        foreach ($all_place as $vv) {
                            //在已有数据中查询，减少数据库查询
                            if ($vv['p_type'] == 2 && $v[1] == $vv['p_name'] && $v[2] == $vv['p_pname']) {
                                $place = $vv;
                            }
                            if ($vv['p_type'] == 1 && $v[2] == $vv['p_name']) {
                                $p_place = $vv;
                            }
                        }
                        if (empty($place)) {
                            $place = db('place')
                                ->where(array('p_name' => $v[1], 'p_type' => 2))
                                ->find();
                            if (empty($p_place)) {
                                $p_place = db('place')
                                    ->where(array('p_name' => $v[2], 'p_type' => 1))
                                    ->find();
                                if (!empty($p_place)) {
                                    $all_place[] = $p_place;
                                }
                            }
                            if (!empty($p_place)) {
                                if (empty($place)) {
                                    $place = array('p_name' => $v[1], 'p_type' => 2, 'p_id' => $p_place['id'], 'p_sort' => $v[4] > 0 ? $v[4] : 0, 'p_status' => $v[3] == '启用' ? 1 : 2);
                                    $res = db('place')->insertGetId($place);
                                    $place['id'] = $res;
                                    $place['p_pname'] = $p_place['p_name'];
                                    $all_place[] = $place;
                                } else {
                                    $all_place[] = $place;
                                }
                            }
                        }
                        unset($place, $p_place);
                    } else if ($v[0] == 3) {
                        //区县级服务地区
                        foreach ($all_place as $vv) {
                            //在已有数据中查询，减少数据库查询
                            if ($vv['p_type'] == 3 && $v[1] == $vv['p_name'] && $v[2] == $vv['p_pname']) {
                                $place = $vv;
                            }
                            if ($vv['p_type'] == 2 && $v[2] == $vv['p_name']) {
                                $p_place = $vv;
                            }
                        }
                        if (empty($place)) {
                            $place = db('place')
                                ->where(array('p_name' => $v[1], 'p_type' => 3))
                                ->find();
                            if (empty($p_place)) {
                                $p_place = db('place')
                                    ->where(array('p_name' => $v[2], 'p_type' => 2))
                                    ->find();
                                if (!empty($p_place)) {
                                    $all_place[] = $p_place;
                                }
                            }
                            if (!empty($p_place)) {
                                if (empty($place)) {
                                    $place = array('p_name' => $v[1], 'p_type' => 3, 'p_id' => $p_place['id'], 'p_sort' => $v[4] > 0 ? $v[4] : 0, 'p_status' => $v[3] == '启用' ? 1 : 2);
                                    $res = db('place')->insertGetId($place);
                                    $place['id'] = $res;
                                    $place['p_pname'] = $p_place['p_name'];
                                    $all_place[] = $place;
                                } else {
                                    $all_place[] = $place;
                                }
                            }
                        }
                        unset($place, $p_place);
                    } else if ($v[0] == 4) {
                        //附近级服务地区
                        foreach ($all_place as $vv) {
                            //在已有数据中查询，减少数据库查询
                            if ($vv['p_type'] == 4 && $v[1] == $vv['p_name'] && $v[2] == $vv['p_pname']) {
                                $place = $vv;
                            }
                            if ($vv['p_type'] == 3 && $v[2] == $vv['p_name']) {
                                $p_place = $vv;
                            }
                        }
                        if (empty($place)) {
                            $place = db('place')
                                ->where(array('p_name' => $v[1], 'p_type' => 4))
                                ->find();
                            if (empty($p_place)) {
                                $p_place = db('place')
                                    ->where(array('p_name' => $v[2], 'p_type' => 3))
                                    ->find();
                                if (!empty($p_place)) {
                                    $all_place[] = $p_place;
                                }
                            }
                            if (!empty($p_place)) {
                                if (empty($place)) {
                                    $place = array('p_name' => $v[1], 'p_type' => 4, 'p_id' => $p_place['id'], 'p_sort' => $v[4] > 0 ? $v[4] : 0, 'p_status' => $v[3] == '启用' ? 1 : 2);
//                                $res = db('place')->insertGetId($place);
//                                $place['id'] = $res;
                                    $place4[] = $place;
                                    $place['p_pname'] = $p_place['p_name'];
                                    $all_place[] = $place;
                                } else {
                                    $all_place[] = $place;
                                }
                            }
                        }
                        unset($place, $p_place);
                    }
                }
            }
            if(!empty($place4) && count($place4)>0){
                db('place')->insertAll($place4);
            }
            return return_json(array('code' => 1, 'msg' => '服务地区添加成功'), '导入服务地区');
        }
    }
}
